Attribute VB_Name = "UserControls"
Option Explicit

'Server Registers New User
Public Sub RegisterNew(ByVal Name As String, ByVal pass As String, Sec_Word As String, Optional IP As String = "127.0.0.1")
    If Not FolderExists(App.Path & "\Users") Then _
        Call MkDir(App.Path & "\Users")
    Call WriteINI("USERDATA", "USERNAME", LCase$(Trim$(Name)), App.Path & "\Users\" & LCase$(Trim$(Name)) & ".ini")
    Call WriteINI("USERDATA", "PASSWORD", Trim$(pass), App.Path & "\Users\" & LCase$(Trim$(Name)) & ".ini")
    Call WriteINI("USERDATA", "SEC_WORD", Trim$(Sec_Word), App.Path & "\Users\" & LCase$(Trim$(Name)) & ".ini")
    Call WriteINI("USERDATA", "USERIP", Trim$(IP), App.Path & "\Users\" & LCase$(Trim$(Name)) & ".ini")
    
    Dim fpath As String
    Dim i As Integer
    fpath = App.Path & "\Users\" & LCase$(Trim$(Name)) & ".ini"
    
    Call WriteINI("USERCHAR", "NAME", vbNullString, fpath)
    Call WriteINI("USERCHAR", "ISCREATED", "False", fpath)
    Call WriteINI("USERCHAR", "CLASS", 0, fpath)
    Call WriteINI("USERCHAR", "SEX", vbNullString, fpath)
    Call WriteINI("USERCHAR", "ACCESS", 0, fpath)
    Call WriteINI("USERCHAR", "LEVEL", 0, fpath)
    Call WriteINI("USERCHAR", "DEF", 0, fpath)
    Call WriteINI("USERCHAR", "STR", 0, fpath)
    Call WriteINI("USERCHAR", "INT", 0, fpath)
    Call WriteINI("USERCHAR", "EXP", 0, fpath)
    Call WriteINI("USERCHAR", "HP", 0, fpath)
    Call WriteINI("USERCHAR", "MAXHP", 0, fpath)
    Call WriteINI("USERCHAR", "MP", 0, fpath)
    Call WriteINI("USERCHAR", "MAXMP", 0, fpath)
    Call WriteINI("USERCHAR", "SPEED", 0, fpath)
    Call WriteINI("USERCHAR", "BODY", 0, fpath)
    Call WriteINI("USERCHAR", "FOOT", 0, fpath)
    Call WriteINI("USERCHAR", "WEAPON", 0, fpath)
    Call WriteINI("USERCHAR", "HANDS", 0, fpath)
    Call WriteINI("USERCHAR", "HEAD", 0, fpath)
    Dim f As Integer
    
    f = FreeFile
    Open App.Path & "\PlayerInvs\" & LCase$(Name) & ".txt" For Output As #f
        For i = 1 To MAX_INV
            Print #f, "0"
            Print #f, "0"
        Next i
    Close #f
    
    f = FreeFile
    Open App.Path & "\PlayerSpells\" & LCase$(Name) & ".txt" For Output As #f
        For i = 1 To MAX_PLAYERSPELLS
            Print #f, "0"
        Next i
    Close #f
    
End Sub

'Check Username *LOGIN & REGISTRATION*
Public Function UserExists(ByVal Name As String) As Boolean
    If FileExists("Users\" & Trim$(Name) & ".ini") Then
        UserExists = True
    Else
        UserExists = False
    End If
End Function

'Check password *LOGIN*
Public Function PassCorrect(ByVal username As String, ByVal Password As String) As Boolean
    Dim pass As String
    pass = ReadINI("USERDATA", "PASSWORD", App.Path & "\Users\" & Trim$(username) & ".ini")
    If pass = Password Then
        PassCorrect = True
    Else
        PassCorrect = False
    End If
End Function

'Check sec word *LOGIN*
Public Function SecWordCorrect(ByVal username As String, ByVal Sec_Word As String) As Boolean
    Dim SecWord As String
    SecWord = ReadINI("USERDATA", "SEC_WORD", App.Path & "\Users\" & Trim$(username) & ".ini")
    If SecWord = Sec_Word Then
        SecWordCorrect = True
    Else
        SecWordCorrect = False
    End If
End Function

'Deletes User By Name
Public Sub DeleteUser(ByVal Name As String)
    If Not UserExists(Trim$(Name)) Then Exit Sub
    Call Kill(App.Path & "\Users\" & Trim$(Name) & ".ini")
End Sub


'Checks if IP is being used
Public Function isIPUsed(ByVal IP As String) As Boolean
    Dim i As Integer
    For i = 1 To MAX_PLAYERS
        If Player(i).IP = IP Then
            isIPUsed = True
            Exit Function
        End If
    Next i
End Function

'Checks if user isn't logged in already
Public Function isUsernameLoggedIn(ByVal username As String) As Boolean
    Dim i As Integer
    Dim S As String
    On Error GoTo err
    For i = 1 To MAX_PLAYERS
        If LCase(Player(i).Login) = LCase(username) Then
            isUsernameLoggedIn = True
            Exit Function
        End If
    Next i
    isUsernameLoggedIn = False
    Exit Function
err:
    isUsernameLoggedIn = False
    Exit Function
End Function

'Is multiple accs logged in
Public Function isMultiLogin(ByVal username As String) As Boolean
    If isUsernameLoggedIn(username) Then
        isMultiLogin = True
    Else
        isMultiLogin = False
    End If
End Function

'User Logout
Public Sub Logout(ByVal index As Integer)
    Dim i As Integer
    If isLoggedIn(index) Then
        i = index
        Player(i).Class = 0
        Player(i).DEF = 0
        Player(i).Exp = 0
        Player(i).Foot = 0
        Player(i).Body = 0
        Player(i).Access = 0
        Player(i).Hands = 0
        Player(i).Head = 0
        Player(i).HP = 0
        Player(i).InGame = False
        Player(i).INT = 0
        Player(i).IP = 0
        Player(i).IsBattling = False
        Player(i).Legs = 0
        Player(i).LEVEL = 0
        Player(i).Login = vbNullString
        Player(i).MAXHP = 0
        Player(i).MAXMP = 0
        Player(i).MP = 0
        Player(i).Name = vbNullString
        Player(i).Password = vbNullString
        Player(i).Sec_Word = vbNullString
        Player(i).Sex = vbNullString
        Player(i).Speed = 0
        Player(i).str = 0
        Player(i).Weapon = 0
        Player(i).created = False
    End If
End Sub

Public Sub SavePlayer(ByVal index As Integer)
    Dim fpath As String
    Dim i As Integer
    fpath = App.Path & "\Users\" & LCase$(Player(index).Login) & ".ini"
    
    Call WriteINI("USERDATA", "USERNAME", LCase$(Player(index).Login), fpath)
    Call WriteINI("USERDATA", "PASSWORD", Player(index).Password, fpath)
    Call WriteINI("USERDATA", "SEC_WORD", Player(index).Sec_Word, fpath)
    Call WriteINI("USERDATA", "USERIP", frmServer.Socket(index).RemoteHostIP, fpath)
    
    Call WriteINI("USERCHAR", "NAME", Player(index).Name, fpath)
    Call WriteINI("USERCHAR", "ISCREATED", Player(index).created, fpath)
    Call WriteINI("USERCHAR", "CLASS", Player(index).Class, fpath)
    Call WriteINI("USERCHAR", "SEX", Player(index).Sex, fpath)
    Call WriteINI("USERCHAR", "ACCESS", Player(index).Access, fpath)
    Call WriteINI("USERCHAR", "LEVEL", Player(index).LEVEL, fpath)
    Call WriteINI("USERCHAR", "DEF", Player(index).DEF, fpath)
    Call WriteINI("USERCHAR", "STR", Player(index).str, fpath)
    Call WriteINI("USERCHAR", "INT", Player(index).INT, fpath)
    Call WriteINI("USERCHAR", "EXP", Player(index).Exp, fpath)
    Call WriteINI("USERCHAR", "HP", Player(index).HP, fpath)
    Call WriteINI("USERCHAR", "MAXHP", Player(index).MAXHP, fpath)
    Call WriteINI("USERCHAR", "MP", Player(index).MP, fpath)
    Call WriteINI("USERCHAR", "MAXMP", Player(index).MAXMP, fpath)
    Call WriteINI("USERCHAR", "SPEED", Player(index).Speed, fpath)
    Call WriteINI("USERCHAR", "BODY", Player(index).Body, fpath)
    Call WriteINI("USERCHAR", "FOOT", Player(index).Foot, fpath)
    Call WriteINI("USERCHAR", "WEAPON", Player(index).Weapon, fpath)
    Call WriteINI("USERCHAR", "HANDS", Player(index).Hands, fpath)
    Call WriteINI("USERCHAR", "HEAD", Player(index).Head, fpath)
    
    Dim f As Integer
    
    f = FreeFile
    Open App.Path & "\PlayerInvs\" & LCase$(Trim$(Player(index).Login)) & ".txt" For Output As #f
        For i = 1 To MAX_INV
            Print #f, Player(index).Inv(i).Item
            Print #f, Player(index).Inv(i).count
        Next i
    Close #f
    
    f = FreeFile
    Open App.Path & "\PlayerSpells\" & LCase$(Player(index).Login) & ".txt" For Output As #f
        For i = 1 To MAX_PLAYERSPELLS
            Print #f, Player(index).Spell(i)
        Next i
    Close #f
End Sub

Public Sub LoginPlayer(ByVal index As Integer, ByVal username As String)
    Dim fpath As String
    
    fpath = App.Path & "\Users\" & LCase$(username) & ".ini"
    Player(index).Login = LCase$(username)
    Player(index).Password = ReadINI("USERDATA", "PASSWORD", fpath)
    Player(index).Sec_Word = ReadINI("USERDATA", "SEC_WORD", fpath)
    Player(index).IP = ReadINI("USERDATA", "USERIP", fpath)
    LoadPlayerChar (index)
End Sub

Public Sub LoadPlayerChar(ByVal index As Integer)
    Dim fpath As String
    Dim i As Integer
    fpath = App.Path & "\Users\" & LCase$(Player(index).Login) & ".ini"
    Player(index).Name = ReadINI("USERCHAR", "NAME", fpath)
    If LCase$(ReadINI("USERCHAR", "ISCREATED", fpath)) = "true" Then _
        Player(index).created = True _
    Else Player(index).created = True
    Player(index).Class = ReadINI("USERCHAR", "CLASS", fpath)
    Player(index).Sex = ReadINI("USERCHAR", "SEX", fpath)
    Player(index).Access = Val(ReadINI("USERCHAR", "ACCESS", fpath))
    Player(index).LEVEL = Val(ReadINI("USERCHAR", "LEVEL", fpath))
    Player(index).DEF = Val(ReadINI("USERCHAR", "DEF", fpath))
    Player(index).str = Val(ReadINI("USERCHAR", "STR", fpath))
    Player(index).INT = Val(ReadINI("USERCHAR", "INT", fpath))
    Player(index).Exp = Val(ReadINI("USERCHAR", "EXP", fpath))
    Player(index).HP = Val(ReadINI("USERCHAR", "HP", fpath))
    Player(index).MAXHP = Val(ReadINI("USERCHAR", "MAXHP", fpath))
    Player(index).MP = Val(ReadINI("USERCHAR", "MP", fpath))
    Player(index).MAXMP = Val(ReadINI("USERCHAR", "MAXMP", fpath))
    Player(index).Speed = Val(ReadINI("USERCHAR", "SPEED", fpath))
    Player(index).Body = Val(ReadINI("USERCHAR", "BODY", fpath))
    Player(index).Foot = Val(ReadINI("USERCHAR", "FOOT", fpath))
    Player(index).Weapon = Val(ReadINI("USERCHAR", "WEAPON", fpath))
    Player(index).Hands = Val(ReadINI("USERCHAR", "HANDS", fpath))
    Player(index).Head = Val(ReadINI("USERCHAR", "HEAD", fpath))
    
    Dim f As Integer
    
    f = FreeFile
    Open App.Path & "\PlayerInvs\" & LCase$(Trim$(Player(index).Login)) & ".txt" For Input As #f
        For i = 1 To MAX_INV
            Input #f, Player(index).Inv(i).Item
            Input #f, Player(index).Inv(i).count
        Next i
    Close #f
    
    f = FreeFile
    Open App.Path & "\PlayerSpells\" & LCase$(Player(index).Login) & ".txt" For Input As #f
        For i = 1 To MAX_PLAYERSPELLS
            Input #f, Player(index).Spell(i)
        Next i
    Close #f
End Sub

Public Sub SaveAllPlayers()
    On Error Resume Next
    Dim i As Integer
    For i = 1 To MAX_PLAYERS
        If isLoggedIn(i) Then
            SavePlayer (i)
        End If
    Next i
End Sub

'Need To Make Player Char Load *DONE*
'Need To Make Player Char Make
'Need To Make Player Join Game
